/*action create by pionner code builder*/

package com.pioneer.app.dev.osuser.user;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.pioneer.app.dev.department.Department;

import config.BaseHibernateDAO;

public class UserlistDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(Userlist.class);
	public Userlist findById(java.lang.Integer id){
		log.debug("getting Userlist instance with id: " + id);
		try {
			Userlist instance =(Userlist)getSession().get("com.pioneer.app.dev.osuser.user.Userlist",id);
			return instance;
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void delete(Userlist persistentInstance){
		log.debug("deletting Userlist persistentInstance  ");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void add(Userlist persistentInstance){
		log.debug("deletting Userlist persistentInstance  ");
		try {
			getSession().save(persistentInstance);
			log.debug("add successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void update(Userlist persistentInstance){
		log.debug("deletting Userlist persistentInstance  ");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public Userlist findByCode(String code){
		log.debug("finding by condition "+code);
		try {

			String queryString =null;
			if(null!=code){
				queryString = "from Userlist where code='"+code+"'";
			}else{queryString = "from Userlist";
			}

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(500);
			List ls=queryObject.list();
			if(null!=ls && ls.size()>0){
				return (Userlist)ls.get(0);
			}else{
				return null;
			}
			
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findByCondition(String condition){
		log.debug("finding by condition "+condition);
		try {

			String queryString =null;
			if(null!=condition){
				queryString = "from Userlist where "+condition;
			}else{queryString = "from Userlist";
			}

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(500);
			return queryObject.list();
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List getByUsername(String userName)
	{
		log.debug("finding by userName "+userName);
		try {

			String queryString =null;
			if(null!=userName){
				queryString = "from Userlist where userName='"+userName+"'";
			}else{queryString = "from Userlist";
			}

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(500);
			return queryObject.list();
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void deleteObjs(java.lang.Integer[]ids){
		log.debug("deleting by objects ");
		Transaction tx=this.getSession().beginTransaction();
		try {
			if(null!=ids){
				Userlist obj=null;
				java.lang.Integer id=null;
				for(int i=0;i<ids.length;i++){	
					id=ids[i];
					obj=this.findById(id);
					
					this.delete(obj);
				}
				tx.commit();
			}
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public Document getUserList(String condition){
		Document doc=null;
		List users=this.findByCondition(condition);
		doc=DocumentFactory.getInstance().createDocument();
		Element root=doc.addElement("TREENODES");
//		root.setAttributeValue("pid", pidStr);
		Userlist obj=null;
		Iterator it=users.iterator();
		Element elt=null;
		while(it.hasNext()){
			obj=(Userlist)it.next();
			elt=root.addElement("TREENODE");
			elt.addAttribute("id", ""+obj.getId().intValue());
			elt.addAttribute("name", obj.getCname());
			elt.addAttribute("mobile", obj.getTelphone());
		}
		return doc;
	}
	
	public String findMaxCode(String proCode){
			String sql="select max(code) from userlist where code like '"+proCode+"%'";
			Query queryObject = getSession().createSQLQuery(sql);
			List ls=queryObject.list();
			String code=(String)ls.get(0);
			return code;
	}
	@Override
	public Session getSession() {
		// TODO Auto-generated method stub
		return super.getSession();
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}
	public static void main(String[] args) throws Exception
	{
		
		
		
		UserlistDAO uld=new UserlistDAO();
		String code=uld.findMaxCode("2008a");
		System.out.println("code="+code);
	}
}
